<template>
  <div>
    <el-dialog
      :loading="loading"
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
      title="新增角色"
    >
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="角色Code" prop="roleCode">
              <el-input
                v-model="formData.roleCode"
                placeholder="请输入角色Code"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色名称" prop="roleName">
              <el-input
                v-model="formData.roleName"
                placeholder="请输入角色名称"
                clearable
                :style="{width: '100%'}"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色父级" prop="roleFcode">
              <RoleSee
                v-model="formData.roleFcode"
                placeholder="请输入角色关键字"
                :style="{width: '100%'}"
                clearable
              ></RoleSee>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属客户端" prop="roleClient">
              <el-select
                v-model="formData.roleClient"
                placeholder="请选择所属客户端"
                clearable
                :style="{width: '100%'}"
              >
                <el-option
                  v-for="(item, index) in roleClientOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import enums from "@/assets/data/enums.js";
import RoleSee from "@/components/RoleSee";
import { put } from "@/api/role";
export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      formData: {
        roleCode: "",
        roleName: "",
        roleClient: "",
        roleFcode: ""
      },
      rules: {
        roleCode: [
          {
            required: true,
            message: "请输入角色Code",
            trigger: "blur"
          }
        ],
        roleName: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur"
          }
        ],
        roleFcode: [
          {
            required: true,
            message: "请选择父角色",
            trigger: "change"
          }
        ],
        roleClient: [
          {
            required: true,
            message: "请选择所属客户端",
            trigger: "change"
          }
        ]
      },
      roleClientOptions: enums.clientType
    };
  },
  methods: {
    onOpen() {},
    onClose() {
      this.$refs["elForm"].resetFields();
      this.$emit("addSuccess", "dept_admin");
    },
    close() {
      this.$emit("update:visible", false);
    },
    handelConfirm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        this.loading = true;
        put(this.formData)
          .then(res => {
            this.loading = false;

            this.close();
          })
          .catch(err => {
            this.loading = false;
            this.close();
          });
      });
    }
  },
  components: {
    RoleSee
  }
};
</script>
<style>
</style>
